"""
时间：2021/03/11
版本号：2.6
重点：登录窗口3
"""
import time
import tkinter as tk
import tkinter.messagebox
import pickle
import joblib #引入可以调用模型的库
import pandas as pd
import xgboost as xgb
interpathy = 300
outputpathy = 330
functionbuttony = 370
root = tk.Tk()  # 实例化对象
root.title('Cell_classification')  # 给窗口取名
root.geometry("700x600")
l = tk.Label(root,text="data模版",font=('Arial'),width=15,
             height=2)  # 标签 用于显示不可编辑的文本或图标
l.pack()    # 放置 放在上下左右
canvas = tk.Canvas(root, height=800, width=1600)
image_file = tk.PhotoImage(file='template.png')
image = canvas.create_image(35, 0, anchor='nw',
                            image=image_file)
canvas.pack(side='top')

tk.Label(root, text='需要预测的文件路径', font=('Arial', 14)).place(x=45, y=interpathy)
tk.Label(root, text='预测文件输出路径', font=('Arial', 14)).place(x=45, y=outputpathy)

var_user_name = tk.StringVar()
var_user_name.set('数据的绝对路径')
print(var_user_name)

entry_user_name = tk.Entry(root, textvariable=var_user_name)
entry_user_name.place(x=180,
                      y=interpathy,
                      width=400)

var_user_password = tk.StringVar()
var_user_password.set('数据的输出路径')

entry_user_password = tk.Entry(root, textvariable=var_user_password)
entry_user_password.place(x=180, y=outputpathy
                          ,width=400)



def predict():
    datapath = var_user_name.get()
    result_path = var_user_password.get()
    """ 输入path """
    # datapath = "data_template.csv"
    # result_path = "data_result1.csv"
    datapath =  datapath
    result_path = result_path
    dataset_use = pd.read_csv(datapath)  # 需要预测的测试集合的导入，更换文件就可以更换test_case了
    dtest = xgb.DMatrix(dataset_use)  # 输入成为xgbDMatrix
    y_pred = model.predict(dtest)  # 输入模型进行验证预测
    y_use = pd.DataFrame(y_pred) + 1  # y数值的预测并且返还标签
    y_use.columns = ['Label']  #
    last_result = pd.concat([dataset_use, y_use], axis=1)
    last_result.to_csv(result_path)
    tk.messagebox.showinfo(title="success", message='结果地址为'+result_path)


def load_modal():
    global model
    model = joblib.load('xgboost.pkl')
    time.sleep(1)
    tk.messagebox.showinfo(title="success", message='模型加载成功')

login = tk.Button(root, text='predict', command=predict).place(x=170, y=functionbuttony,width=120,height=40)
sign_up = tk.Button(root, text='load_modal', command=load_modal).place(x=340, y=functionbuttony,width=120,height=40)

root.mainloop()  # 大型的while循环




# def user_login():
#     user_name = var_user_name.get()
#     user_pssword = var_user_password.get()
#     try:
#         with open("usrs_info.pickle", 'rb') as user_file:
#             usrs_info = pickle.load(user_file)
#     except FileNotFoundError:
#         with open('usrs_info.pickle', 'wb') as user_file:
#             usrs_info = {'admin': 'admin'}
#             pickle.dump(usrs_info, user_file)
#
#     if user_name in usrs_info:
#         if user_pssword == usrs_info[user_name]:
#             tk.messagebox.showinfo(title="Welcome", message='How are you?' + user_name)
#         else:
#             tk.messagebox.showerror(message="Error your password is wrong,try again!")
#     else:
#         is_sign_up = tk.messagebox.askyesno(title='Hi', message='You have not sign up yet,sign up today?')
#
#         if is_sign_up:
#             user_sign_up()


# def load_modal():
#     def sign_up_admin():
#         np = new_psd.get()
#         npf = new_psd_confirm.get()
#         nn = new_name.get()
#
#         with open('usrs_info.pickle', 'rb') as user_file:
#             exit_user_info = pickle.load(user_file)
#         if np != npf:
#             tk.messagebox.showerror(title="Error", message='Password and confirm password must be the same!')
#         elif nn in exit_user_info:
#             tk.messagebox.showerror(title="Error", message='The user has already signed up!')
#         else:
#             exit_user_info[nn] = np
#             with open('usrs_info.pickle', 'wb') as user_file:
#                 pickle.dump(exit_user_info, user_file)
#             tk.messagebox.showinfo(title='Welcome', message="You have successfully signed up!")
#             root_sign_up.destroy()
#
#     root_sign_up = tk.Toplevel(root)
#     root_sign_up.geometry('350x200')
#     root_sign_up.title('Sign up root')
#
#     new_name = tk.StringVar()
#     new_name.set('example@gmail.com')
#     tk.Label(root_sign_up, text="User name:").place(x=10, y=10)
#     entry_new_name = tk.Entry(root_sign_up, textvariable=new_name).place(x=120, y=10)
#
#     new_psd = tk.StringVar()
#     new_psd.set('')
#     tk.Label(root_sign_up, text="Password:").place(x=10, y=60)
#     entry_new_psd = tk.Entry(root_sign_up, textvariable=new_psd, show='*').place(x=120, y=60)
#
#     new_psd_confirm = tk.StringVar()
#     new_psd_confirm.set('')
#     tk.Label(root_sign_up, text="Confirm Password:").place(x=10, y=110)
#     entry_new_psd_confirm = tk.Entry(root_sign_up, textvariable=new_psd_confirm, show='*').place(x=120, y=110)
#
#     btn_comfirm_sign_up = tk.Button(root_sign_up, text='Sign up', command=sign_up_admin)
#     btn_comfirm_sign_up.place(x=170, y=160)
